package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     * @param dto
     */
    @Override
    public void add(ShoppingCartDTO dto) {
        // 创建ShoppingCart对象
        ShoppingCart shoppingCart = new ShoppingCart();
        // 拷贝属性值
        BeanUtils.copyProperties(dto,shoppingCart);

        // 1.判断该商品是否已经存在购物车中--条件：dishId+dishFlavor+userId
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart cart = shoppingCartMapper.selectBy(shoppingCart);
        if(cart == null){ //代表购物车没有该商品
            // 2.补充缺失的属性值
            // 判断是新增套餐还是新增菜品
            if(dto.getDishId()!=null){
                // 新增菜品:根据菜品的id查询菜品表，获取菜品相关信息
                Dish dish = dishMapper.selectById(dto.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
            }else{
                // 新增套餐:根据套餐的id查询套餐表，获取套餐相关信息
                Setmeal setmeal = setmealMapper.getById(dto.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            //3.调用mapper，保存数据
            shoppingCartMapper.insert(shoppingCart);
        }else{ //代表购物车已经存在该商品
            //4.将原来的购物车商品数量+1，调用mapper更新方法
            cart.setNumber(cart.getNumber()+1);
            shoppingCartMapper.update(cart);
        }
    }

    /**
     * 查询购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        return shoppingCartMapper.list(BaseContext.getCurrentId());
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        shoppingCartMapper.delete(BaseContext.getCurrentId());
    }

    @Override
    public void deleteOne(ShoppingCartDTO dto) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto,shoppingCart);
        //设置查询条件，查询当前登录用户的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> cartDBList = shoppingCartMapper.listBy(shoppingCart);
        if(cartDBList != null && cartDBList.size()>0){
            ShoppingCart cartDB =  cartDBList.get(0);
            Integer number = cartDB.getNumber();
            if(number == 1){
                //如果数量为1，直接删除
                shoppingCartMapper.deleteById(cartDB.getId());
            }else{
                //如果数量大于1，只需要将数量-1即可
                cartDB.setNumber(cartDB.getNumber()-1);
                shoppingCartMapper.update(cartDB);
            }
        }

    }
}
